﻿using AutoMapper;
using B.S.Warehouse.Domain;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.WarehouseManagementCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.WarehouseManagementCommandHandler
{
    public class UpdateStorageAreaCommandHandler : IRequestHandler<UpdateStorageAreaCommand, ApiResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdateStorageAreaCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<ApiResult<object>> Handle(UpdateStorageAreaCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnum.修改成功;
            result.MAG = "修改成功";

            var model = db.Queryable<StorageArea>().Where(p => p.StorageAreaId == request.StorageAreaId);
            if (model.Count() != 0)
            {
                var production = mapper.Map<StorageArea>(request);
                await db.Updateable<StorageArea>(production).ExecuteCommandAsync();
            }

            return await Task.FromResult(result);
        }
    }
}
